/*
    RExLib project, file drimysql.cpp
    Copyright (C) 2012  Sarvaritdinov Ravil ra9oaj@gmail.com

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#include "drimysql.h"

const char* drimysql::nativeErrorString() const
{
  if ( !_mconnector )
    return 0;
  return mysql_error ( _mconnector );
}

int drimysql::nativeErrorNo() const
{
  if ( !_mconnector )
    return 0;
  return mysql_errno ( _mconnector );
}

bool drimysql::seek ( long long unsigned int pos )
{
  mysql_data_seek ( _mresult, pos );
  if ( !mysql_row_tell ( _mresult ) )
    return false;

  _gpos = pos;
  return true;
}

bool drimysql::first()
{
  mysql_data_seek ( _mresult, 0 );
  if ( !mysql_row_tell ( _mresult ) )
    return false;

  _gpos = 0;
  return true;
}

bool drimysql::next()
{
  if ( !_mresult )
    return false;
  mysql_data_seek ( _mresult,++_gpos );
  if ( !mysql_row_tell ( _mresult ) )
    return false;
  return true;
}

int drimysql::query ( const char* qry )
{
  if ( _gstate != DB_CONNECTED )
    return -1;

  if ( _mresult )
    mysql_free_result ( _mresult );
  if ( !mysql_query ( _mconnector, qry ) )
    {
      _mresult = mysql_store_result ( _mconnector );
      _gpos = 0;
      return 0;
    }
  else
    return -1;
}

unsigned int drimysql::fieldCount() const
{
  if ( ! _mresult )
    return 0;
  return mysql_num_fields ( _mresult );
}

long long unsigned int drimysql::affectedRows() const
{
  if ( !_mresult )
    return 0;
  return mysql_affected_rows ( _mconnector );
}

long long unsigned int drimysql::rowCount() const
{
  if ( !_mresult )
    return 0;
  return mysql_num_rows ( _mresult );
}

char* drimysql::value ( int val )
{
  if ( !_mresult )
    return 0;
  MYSQL_ROW_OFFSET cur = mysql_row_tell ( _mresult );
  if ( !cur )
    return 0;
  MYSQL_ROW row = cur->data;
  if ( !cur )
    return 0;
  return row[val];
}

int drimysql::dbState() const
{
  return _gstate;
}

bool drimysql::closeDB()
{
  _gpos = 0;
  if ( _mresult )
    {
      mysql_free_result ( _mresult );
      _mresult = 0x0;
    }

  mysql_close ( _mconnector );
  _mconnector = 0;
  _gstate = DB_UNCONNECTED;
  return true;
}

bool drimysql::openDB ( const char* host, uint port )
{
  if ( _gstate == DB_CONNECTED )
    return false;

  _gpos = 0;
  if ( !port )
    port = 3306;

  mysql_init ( &_mysql );
  if ( !_unix_sock )
    _mconnector = mysql_real_connect ( &_mysql,host,_username,_passwd,_dbname,port,0,0 );
  else
    _mconnector = mysql_real_connect ( &_mysql,0,_username,_passwd,_dbname,port,host,0 );

  if ( !_mconnector )
    {
      _gstate = DB_ERROR;
      _gerror = DBE_CONNECT;
      return false;
    }

  _gerror = DBE_NOERROR;
  _gstate = DB_CONNECTED;
  return true;
}

void drimysql::setUnixConnection ( bool unixf )
{
  if ( _gstate == DB_CONNECTED && _unix_sock != unixf )
    {
      printWarning ( "Can not change the connection type of the database during a connection.\r\n" );
      return;
    }

  _unix_sock = unixf;
}

void drimysql::setSSLConnection ( bool sslf )
{
  if ( _gstate == DB_CONNECTED && _ssl != sslf )
    {
      printWarning ( "Can not change the connection type of the database during a connection.\r\n" );
      return;
    }

  _ssl = sslf;
}

void drimysql::setDBUser ( const char* username, const char* passw )
{
  if ( _gstate == DB_CONNECTED )
    {
      printWarning ( "Can not change the username and password of the database during a connection.\r\n" );
      return;
    }

  if ( _username )
    {
      delete[] _username;
      _username = 0;
    }

  if ( _passwd )
    {
      delete[] _passwd;
      _passwd = 0;
    }

  if ( username )
    {
      _username = new char[strlen ( username )];
      strcpy ( _username,username );
    }

  if ( passw )
    {
      _passwd = new char[strlen ( passw )];
      strcpy ( _passwd,passw );
    }
}

void drimysql::setDBName ( const char* name )
{
  if ( _gstate == DB_CONNECTED )
    {
      printWarning ( "Can not change the name of the database during a connection.\r\n" );
      return;
    }

  if ( _dbname )
    {
      delete[] _dbname;
      _dbname = 0;
    }

  if ( !name )
    return;

  _dbname = new char[strlen ( name )];
  strcpy ( _dbname,name );
}

drimysql::drimysql()
{
  _gstate = DB_UNCONNECTED;
  _gerror = DBE_NOERROR;
  _unix_sock = false;
  _mstate = 0;
  _gpos = 0;
  _ssl = false;
  _username = 0;
  _passwd = 0;
  _lastquery = 0;
  _dbname = 0;
  _mresult = 0x0;
  _mconnector = 0;
}

drimysql::drimysql ( const drimysql& other )
{

}

drimysql::~drimysql()
{
  if ( _gstate == DB_CONNECTED )
    closeDB();
}

int drimysql::errorNo() const
{
  return _gerror;
}

void drimysql::printWarning ( const char* msg )
{
  printf ( "(W)DBConnector: %s",msg );
}

void drimysql::printError ( const char* msg )
{
  printf ( "(E)DBConnector: %s",msg );
}

const char* drimysql::errorString() const
{
  return 0;
}

int drimysql::errorCode() const
{
  return 0;
}

